#!/bin/bash
# img_file="tmp/deploy/images/qemux86/core-image-apd-devel-fusion-qemux86.ext4"
# kernel_file="tmp/deploy/images/qemux86/bzImage-qemux86.bin"
# apd的qemu
# cmd="tmp/work/x86_64-linux/qemu-helper-native/1.0-r1/recipe-sysroot-native/usr/bin/qemu-system-i386"
function usage() {
    echo "Usage: apd_run.sh <apd_image> <apd_kernel> <tap_dev> <ip_addr>"
    echo -e "\t <ip_addr> should be 192.168.7.4 for fusion image, 192.168.7.2 for other image"
    exit -1
}

if [ $# -ne 4 ]
then 
    usage
fi

img_file="$1"
kernel_file="$2"
tap_dev="$3"
ip_addr="$4"
if [ "192.168.7.4" != "${ip_addr}" -a "192.168.7.2" != "${ip_addr}" ]
then
    usage
fi

mac_addr=$( printf '52:54:00:12:%02X:%02X\n' $((RANDOM%256)) $((RANDOM%256)) )

# 宿主机的qemu
cmd="qemu-system-i386"

append_param="root=/dev/vda rw"
append_param+=" console=ttyS0" 
append_param+=" mem=256M" 
append_param+=" ip=${ip_addr}::192.168.7.1:255.255.255.0"
append_param+=" oprofile.timer=1"
append_param+=" rootfstype=ext4"
# append_param+=" vga=0"
# append_param+=" vesafb.mode_option=640x480-32"
# append_param+=" uvesafb.task_timeout=-1"

${cmd} \
    -device e1000,netdev=network0,mac=${mac_addr} \
    -netdev tap,id=network0,ifname=${tap_dev},script=no,downscript=no \
    -drive file=${img_file},if=virtio,format=raw \
    -kernel ${kernel_file} \
    -cpu qemu32 \
    -no-reboot \
    -m 256 \
    -nographic \
    -append "${append_param}" \
    -object rng-random,filename=/dev/urandom,id=rng0 \
    -device virtio-rng-pci,rng=rng0 \
    -name fusion 
